#include "timer.h"
#include <sstream>

Timer::Timer(){
    this->m_name = "Timer";
    this->m_started = false;
    this->n = 0;
}

Timer::Timer(string name){
    this->m_name = name;
    this->m_started = false;
    this->n = 0;
}

void Timer::start(){
    if(m_started){
        cout << "Timer should not start twice successively." << this->m_name << endl;
        throw 1;
    }
    m_StartTime = chrono::system_clock::now();
    m_started = true;
}

void Timer::pause(){
    if(!m_started){
        cout << "Timer not started." << this->m_name << endl;
        throw 1;
    }
    m_started = false;
    auto endTime = chrono::system_clock::now();
    m_ellapsed_millisecs += chrono::duration_cast<chrono::milliseconds>(endTime - m_StartTime).count();
    this->n += 1;
}

void Timer::reset(){
    m_started = false;
    n = 0;
    m_ellapsed_millisecs = 0;
}


double Timer::elapsedMilliseconds(){
    return this->m_ellapsed_millisecs;
}

double Timer::elaspsedSeconds(){
    return this->m_ellapsed_millisecs / 1000;
}

string Timer::output(){
    stringstream ss;
    ss << "Timer->" << m_name << ": " << "average=" << m_ellapsed_millisecs / (n + 1e-8) << "millisecs; total=" << m_ellapsed_millisecs << "millisecs";
    return ss.str();
}